package com.yc.springmvc.web;

import com.yc.springmvc.bean.Hall;
import com.yc.springmvc.bean.Movie;
import com.yc.springmvc.bean.Plan;
import com.yc.springmvc.bean.Result;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.annotation.MultipartConfig;
import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController{

    @RequestMapping("gohome.do")
    String gohome(){
        return "redirect:home.html";
    }

    //  ResponseBody 指定返回对象转成 json 格式
    @ResponseBody
    @RequestMapping("result.do")
    Result getResult(){
        return new Result(1,"now", new Date());
    }

    // 127.0.0.1:8080/工程名/showName?myname=xxxx
    @RequestMapping("showName")
    @ResponseBody
    public String showName(
            // mybatis = @Param, 结果集  @Results + @Result, <resultMap>
            @RequestParam("myname") String name,
            @RequestParam(value="myage", required = true, defaultValue = "18") int age){
        return "my name is " + name + ", age = " + age;
    }

    // 地址参数
    // 127.0.0.1:8080/工程名/user/180/abc
    @ResponseBody
    @RequestMapping("user/{myid}/{mysn}")
    public String myId(@PathVariable("myid") int id,
                       @PathVariable("mysn") String sn){
        return "my id is " + id + ", my sn is " + sn;
    }

    @ResponseBody
    @RequestMapping("headerParam")
    public String headerParam(
            @RequestHeader String Host,
            @RequestHeader(value="Accept-Encoding")String acceptEncoding,
            @RequestHeader(value="Content-Type", defaultValue = "text")String contentType,
            @RequestHeader String Cookie,
            @CookieValue String myname

    ){
        return "Host="+Host + ", " +
                "acceptEncoding="+acceptEncoding + ", " +
                "contentType="+contentType + ", " +
                "Cookie="+Cookie + ", " +
                "myname="+myname;
    }

    @RequestMapping("toth.do")
    public String toThymeleaf(Model data){
        data.addAttribute("msg","欢迎来到 Thymeleaf 的世界");
        // /WEB-INF/templates/index.html
        return "index";
    }

    //127.0.0.1/xxx/halls.do?el=table  => 表格中展示数据
    //127.0.0.1/xxx/halls.do?el=ul  => 列表中展示数据
    @RequestMapping("halls.do")
    // 如果没有方法的返回值定义, 那么方法名即视图名
    // 如果没有方法的返回值定义, 那么访问地址的文件名前缀既是模板名
    public void showHall(Map<String,Object> data){
        List<Hall> halls = new ArrayList<>();
        halls.add(new Hall(1,"豪华3D", true,"大",(short)100));
        halls.add(new Hall(2,"豪华2D", true,"大",(short)110));
        halls.add(new Hall(3,"豪华4D", true,"小",(short)120));
        halls.add(new Hall(4,"豪华1D", true,"大",(short)130));
        data.put("halls", halls);
    }

    @RequestMapping("plan.do")
    public void toplan(){
    }

    @ResponseBody
    @RequestMapping("addPlan")
    public Plan addPlan( Plan plan){
        return plan;
    }

    @ResponseBody
    @RequestMapping("addMovie")
    public Result addMovie(@Valid Movie movie, Errors errors){
        if(errors.hasFieldErrors()){
            errors.getFieldErrors().forEach(System.out::println);
            return new Result(0,"字段验证错误", errors.getFieldErrors());
        }
        return new Result(1,"添加成功!",null);
    }

    /**
     *
     * @param @MultipartFile file Spring 封装文件上传对象
     * @return
     */
    @ResponseBody
    @PostMapping("uploadImage")
    public Result uploadImage(@RequestParam("image") MultipartFile file)
            throws IOException {
        // 文件名 => xxx.jpg
        final String originalFilename = file.getOriginalFilename();
        // 字段名 => image
        file.getName();
        file.transferTo(new File("D:\\_文件柜\\123\\0609/"+originalFilename));
        String webPath = "upload_image/" + originalFilename;
        return new Result(1, "文件上传成功!", webPath);
    }

    @ResponseBody
    @RequestMapping("nologin.action")
    // 请求转发跳转 的 控制器方法, 必须加 public
    public Result nologin(){
        return new Result(0,"请先登录系统",null);
    }

}
